package br.ufal.tci.nexos.arcolive.tests;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.beans.Introspector;
import java.io.IOException;

import javax.media.CannotRealizeException;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.MediaLocator;
import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.media.RealizeCompleteEvent;
import javax.swing.JInternalFrame;

import br.ufal.tci.nexos.arcolive.beans.ArCoLIVEProcessor;

/**
 * LVSPlayerInternalFrame.java
 *
 * <p>This class is a coupling to LVSPlayer class.</p>
 *
 *
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de Sales</a>.
 * @since 0.1
 * @version 0.1
 *
 * <p><b>Revisions:</b>
 *
 * <p><b>20050317 leandro e felipe</b>
 * <ul>
 * <li> First version
 * </ul>
 */

public class ArCoLIVEProcessorInternalFrame extends JInternalFrame implements ControllerListener {

	private ArCoLIVEProcessor lvsProcessor;
	private String sourcePath;
	private Component videoComponent, controlComponent;
	
	public ArCoLIVEProcessorInternalFrame() {
		super();
		String[] beanInfosPath = {"br.ufal.tci.arcolive.beans.info"}; 
		Introspector.setBeanInfoSearchPath(beanInfosPath);
		initGUI();
		this.lvsProcessor = new ArCoLIVEProcessor();
	}
	
	public void setSourcePath(String path) {
		this.sourcePath = path;
		this.lvsProcessor.setMediaLocator(new MediaLocator(path));
	}
	
	public String getSourcePath() {
		return this.sourcePath;
	}
	
	public void play() throws NoDataSourceException, NoPlayerException, IOException, CannotRealizeException {
		//this.lvsProcessor.createProcessor();
		this.lvsProcessor.addControllerListener(this);
		//this.lvsProcessor.play();
	}
	
	public ArCoLIVEProcessor getLvsProcessor() {
		return this.lvsProcessor;
	}
		
	private void initGUI() {
		try {
			setTitle("LVSProcessor");
			setIconifiable(true);
			setClosable(true);
			setMaximizable(true);
			setResizable(true);
			getContentPane().setLayout(new BorderLayout());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void controllerUpdate(ControllerEvent event) {
		if (event instanceof RealizeCompleteEvent) {
			this.videoComponent = this.lvsProcessor.getControlComponent(); 
			if (this.videoComponent != null) {
				getContentPane().add(this.videoComponent, BorderLayout.SOUTH);
			}
			this.controlComponent = this.lvsProcessor.getVisualComponent(); 
			if (this.controlComponent != null) {
				getContentPane().add(this.controlComponent, BorderLayout.CENTER);
			}
			validate();
		}
				
	}
	
	public Dimension getPreferredSize() {
        int w = 0, h = 0;
        if (this.videoComponent != null) {
            Dimension size = this.videoComponent.getPreferredSize();
            w = size.width;
            h = size.height;
        }
        if (this.controlComponent != null) {
            Dimension size = this.controlComponent.getPreferredSize();
            if (w == 0)
                w = size.width;
            h += size.height;
        }
        if (w < 160)
            w = 160;
        return new Dimension(w, h);
    }

}